package
{
	import com.xcopy.Screen;
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.events.Event;

	public class Slot25FreeGameJungle extends Screen
	{
		public const TERMINATE:String            = 'TERMINATE';
		public const STATE_NONE:Number           = 0;
		public const STATE_WELCOME:Number        = 1;
		public const STATE_PICK:Number           = 2;
		public const STATE_COLLECT:Number        = 3;
		public const STATE_TAKE:Number           = 4;
		public const STATE_OVER:Number           = 5;
		public const STATE_AUTO:Number           = 6;
		public const TIME_MAX:Number             = 30;
		public const TIME_WELCOME:Number         = 2.0;
		public const TIME_BREAK_AND_FLARE:Number = 0.4;
		public const TIME_BREAK_COLLECT:Number   = 0.8;
		public const TIME_BOUNTY_COLLECT:Number  = 0.3;
		public const TIME_DEMO_COLLECT:Number    = 9.0;
		public const TIME_FLARE_5:Number         = 0.6;
		public const TIME_AUTO_OPEN:Number       = 0.9;
		private var _state:Number                = STATE_NONE;
		private var _congratulation:MovieClip;
		private var _welcome:MovieClip;
		private var _timeup:MovieClip;
		private var _stone:MovieClip;
		private var _boxBounty:DigitalBox        = new DigitalBox(this);
		private var _boxTime:DigitalBox          = new DigitalBox(this);
		private var _current:Number              = 0;
		private var _queue:Array                 = []; // 結果陣列
		private var _reward:Number               = 0;
		private var _position:Number             = 0;
		private var _madel:Number                = 0;
		private var _buttons:Array               = [];
		private var _breaks:Array                = [];
		private var _bounties:Array              = []; // 彈出數字
		private var _got:Array                   = []; // 已獲得寶物
		private var _statistics:Array            = []; // 已獲得寶物統計
		private var _pos:Array                   = []; // 開啟的位置內容物（場中火花要用）
		public var data:Object;
		private var _machine:Slot25Machine;

		public function get machine():Slot25Machine
		{
			return _machine;
		}

		public function get table():Slot25Table
		{
			return machine.table;
		}

		public function get debug():Slot25Debugger
		{
			return machine.debug;
		}

		public function Slot25FreeGameJungle(r:* = null)
		{
			super(r);
			_machine = r as Slot25Machine;
		}

		// 初始化物件
		override public function set clip(mc:MovieClip):void
		{
			super.clip = mc;
			_congratulation = clip.oCongratulation;
			_welcome = clip.oWelcome;
			_stone = clip.oStone;
			_timeup = clip.oTimeup;
			_boxBounty.clip = _congratulation.oMoney;
			_boxTime.clip = clip.oTime;
			var mv:MovieClip;
			var bt:SimpleButton;
			var i:Number = 0;
			var j:Number = 0;
			for (i = 0; i < 15; i++)
			{
				bt = _stone.getChildByName('btnStone' + (i + 1)) as SimpleButton;
				setClickEvent(bt, createCallBack(onClick, i));
				_buttons[i] = bt;
				mv = _stone.getChildByName('oBreak' + (i + 1)) as MovieClip;
				_breaks[i] = mv;
				mv = _stone.getChildByName('oBounty' + (i + 1)) as MovieClip;
				_bounties[i] = mv;
			}
			_got = [];
			for (i = 0; i < 4; i++)
			{
				_got[i] = [];
				for (j = 0; j < 5; j++)
				{
					mv = clip.getChildByName('oGot' + i + '' + j) as MovieClip;
					_got[i][j] = mv;
				}
			}
		}

		// 開啟石板後，所有同樣的寶物隔一段時間發亮
		public function flareCollections(face:Number):void
		{
			var i:Number = 0;
			for (i = 0; i < 5; i++)
			{
				_got[face][i].oFlare.visible = true;
				_got[face][i].oFlare.gotoAndPlay('Start');
			}
		}

		public function flareItems(face:Number):void
		{
			var i:Number = 0;
			for (i = 0; i < 15; i++)
			{
				if (_pos[i] == face)
				{
					_breaks[i].oItem.oAni.oFlare.visible = true;
					_breaks[i].oItem.oAni.oFlare.gotoAndPlay('Start');
				}
			}
		}

		// 開啟石板後，底下的寶物隔一段時間發亮
		public function flareItem(index:Number):void
		{
			playSound('1310');
			_breaks[index].oItem.oAni.oFlare.visible = true;
			_breaks[index].oItem.oAni.oFlare.gotoAndPlay('Start');
		}

		// 開啟石板後，底下的寶物順便彈出金額
		public function bountyItem(index:Number, value:Number):void
		{
			//trace(value);
			_bounties[index].visible = true;
			_bounties[index].oNumber.txtValue.text = value;
			_bounties[index].gotoAndPlay('Start');
		}

		// 開啟石板後，底下的寶物順便彈出金額
		public function bountyCollect(index:Number):void
		{
			playSound('1330');
			_bounties[index].gotoAndPlay('Collect');
			_boxBounty.target += Number(_bounties[index].oNumber.txtValue.text);
		}

		// 開啟石板後，右方的統計隔一段時間彈出
		public function gotItem(face:Number):void
		{
			_got[face][_statistics[face]].visible = true;
			_got[face][_statistics[face]].gotoAndPlay('Got');
			_statistics[face]++;
			playSound('1320');
			if (_current < _position && state != STATE_AUTO)
			{
				state = STATE_PICK;
			}
		}

		public function addBounty(n:Number):void
		{
			_boxBounty.number = n;
			if (n == 0)
			{
				_boxBounty.target = 0;
			}
			else
			{
				playSound('2010');
			}
			this.machine.addJungleMoney(n);
		}

		public function set timeless(n:Number):void
		{
			_boxTime.number = n;
		}

		public function get timeless():Number
		{
			return _boxTime.number;
		}

		public function set state(n:Number):void
		{
			_state = n;
			var i:Number = 0;
			switch (_state)
			{
				case STATE_PICK:
				{
					for (i = 0; i < 15; i++)
					{
						_buttons[i].enabled = true;
					}
					break;
				}
				case STATE_COLLECT:
				case STATE_AUTO:
				case STATE_OVER:
				{
					for (i = 0; i < 15; i++)
					{
						_buttons[i].enabled = false;
					}
					break;
				}
			}
		}

		public function get state():Number
		{
			return _state;
		}

		// 初始化畫面，右方清空，button 清空，break 清空
		public function welcome():void
		{
			var i:Number = 0;
			var j:Number = 0;
			this.debug.print('小遊戲歡迎畫面');
			state = STATE_WELCOME;
			playSound('2000');
			_welcome.visible = true;
			_welcome.gotoAndPlay('Start');
			_congratulation.visible = false;
			_timeup.visible = false;
			timeless = TIME_MAX;
			_boxTime.enable();
			for (i = 0; i < 15; i++)
			{
				_buttons[i].visible = false;
				_breaks[i].visible = false;
				_bounties[i].visible = false;
			}
			for (i = 0; i < 4; i++)
			{
				for (j = 0; j < 5; j++)
				{
					_got[i][j].visible = false;
				}
			}
			fadeEnable();
			//enable();
			addCaller(this, {time: TIME_WELCOME, count: 1, onUpdate: start});
		}

		public function playSound(name:String):void
		{
			this.table.playSound(name);
		}

		public function start():void
		{
			state = STATE_PICK;
			this.table.music = 'Free';
			addBounty(0);
			_welcome.visible = false;
			_stone.gotoAndPlay('Start');
			_current = 0;
			_queue = [];
			_pos = [];
			var i:Number = 0;
			var j:Number = 0;
			for (i = 0; i < data.Cards.length; i++)
			{
				_queue[i] = {Face: data.Cards[i], Gold: data.Gold[i]};
				_buttons[i].visible = true;
				_pos[i] = -1;
			}
			_reward = data.Total;
			_position = data.Position;
			_madel = data.Madel;
			//trace(_madel);
			//trace(_reward);
			for (i = 0; i < 4; i++)
			{
				_statistics[i] = 0;
				for (j = 0; j < 5; j++)
				{
					_got[i][j].visible = false;
					_got[i][j].oFlare.gotoAndStop('End');
				}
			}
			addTween(this, {timeless: 0, time: TIME_MAX, onComplete: timeup, transition: 'linear'});
		}

		// 時間到！開始自動開板
		public function timeup():void
		{
			state = STATE_AUTO;
			_timeup.visible = true;
			_timeup.gotoAndPlay('Start');
			_boxTime.disable();
			//trace('Time up');
			addAutoOpen();
		}

		// 開啟一個石板
		public function doAutoOpen():void
		{
			if (state != STATE_AUTO)
			{
				return;
			}
			var i:Number     = 0;
			var index:Number = 0;
			for (i = 0; i < 15; i++)
			{
				if (_pos[i] == -1)
				{
					index = i;
					i = 20;
				}
			}
			playSound('1300');
			// 按鈕消失
			_buttons[index].visible = false;
			_pos[index] = _queue[_current].Face;
			// 動畫出現
			_breaks[index].visible = true;
			_breaks[index].oStone.gotoAndPlay('Start');
			_breaks[index].oItem.gotoAndStop('Item' + _queue[_current].Face);
			_breaks[index].oItem.oAni.oFlare.visible = false;
			//_statistics[_queue[_current].Face]++;
			addCaller(_breaks[index], {time: TIME_BREAK_AND_FLARE, count: 1, onUpdate: bountyItem, onUpdateParams: [index, _queue[_current].Gold]});
			addCaller(_breaks[index], {time: TIME_BREAK_AND_FLARE, count: 1, onUpdate: flareItems, onUpdateParams: [_queue[_current].Face]});
			addCaller(_breaks[index], {time: TIME_BREAK_COLLECT, count: 1, onUpdate: gotItem, onUpdateParams: [_queue[_current].Face]});
			_current++;
			// 判斷是否已開到最後一個
			if (_current == _position)
			{
				//trace('開到尾巴了！！！');
				removeTween(this, timeless);
				//addTween(this, {timeless : 0, time : 30, onComplete : timeup,
				//transition : 'linear'});
				state = STATE_OVER;
				addCaller(this, {time: TIME_FLARE_5, count: 1, onUpdate: flare});
				return;
			}
			addAutoOpen();
		}

		public function addAutoOpen():void
		{
			addCaller(this, {time: TIME_AUTO_OPEN, count: 1, onUpdate: doAutoOpen});
		}

		// 開始放煙火囉！
		public function flare():void
		{
			_congratulation.visible = true;
			addCaller(_stone, {time: TIME_BREAK_COLLECT + TIME_BREAK_AND_FLARE * 1, count: 1, onUpdate: flareCollections, onUpdateParams: [_madel]});
			addCaller(_stone, {time: TIME_BREAK_COLLECT + TIME_BREAK_AND_FLARE * 2, count: 1, onUpdate: flareCollections, onUpdateParams: [_madel]});
			addCaller(_stone, {time: TIME_BREAK_COLLECT + TIME_BREAK_AND_FLARE * 3, count: 1, onUpdate: flareCollections, onUpdateParams: [_madel]});
			addCaller(_stone, {time: TIME_BREAK_COLLECT + TIME_BREAK_AND_FLARE * 4, count: 1, onUpdate: flareCollections, onUpdateParams: [_madel]});
			addCaller(_stone, {time: TIME_BREAK_COLLECT + TIME_BREAK_AND_FLARE * 5, count: 1, onUpdate: flareCollections, onUpdateParams: [_madel]});
			addCaller(_stone, {time: TIME_BREAK_COLLECT + TIME_BREAK_AND_FLARE * 1, count: 1, onUpdate: playSound, onUpdateParams: ['1310']});
			addCaller(_stone, {time: TIME_BREAK_COLLECT + TIME_BREAK_AND_FLARE * 3, count: 1, onUpdate: playSound, onUpdateParams: ['1310']});
			addCaller(_stone, {time: TIME_BREAK_COLLECT + TIME_BREAK_AND_FLARE * 5, count: 1, onUpdate: playSound, onUpdateParams: ['1310']});
			addCaller(_stone, {time: TIME_BREAK_COLLECT + TIME_BREAK_AND_FLARE * 1, count: 1, onUpdate: flareItems, onUpdateParams: [_madel]});
			addCaller(_stone, {time: TIME_BREAK_COLLECT + TIME_BREAK_AND_FLARE * 2, count: 1, onUpdate: flareItems, onUpdateParams: [_madel]});
			addCaller(_stone, {time: TIME_BREAK_COLLECT + TIME_BREAK_AND_FLARE * 3, count: 1, onUpdate: flareItems, onUpdateParams: [_madel]});
			addCaller(_stone, {time: TIME_BREAK_COLLECT + TIME_BREAK_AND_FLARE * 4, count: 1, onUpdate: flareItems, onUpdateParams: [_madel]});
			addCaller(_stone, {time: TIME_BREAK_COLLECT + TIME_BREAK_AND_FLARE * 5, count: 1, onUpdate: flareItems, onUpdateParams: [_madel]});
			var i:Number = 0;
			var j:Number = 0;
			for (i = 0; i < 15; i++)
			{
				if (_pos[i] == _madel)
				{
					addCaller(_stone, {time: TIME_BREAK_COLLECT + TIME_BREAK_AND_FLARE * 5 + TIME_BOUNTY_COLLECT * j, count: 1, onUpdate: bountyCollect,
							onUpdateParams: [i]});
					j++;
				}
			}
			addCaller(_stone, {time: TIME_BREAK_COLLECT + TIME_BREAK_AND_FLARE * 5 + TIME_BOUNTY_COLLECT * 6, count: 1, onUpdate: addBounty,
					onUpdateParams: [_reward]});
			addCaller(this, {time: TIME_DEMO_COLLECT, count: 1, onUpdate: end});
		}

		public function end():void
		{
			this.table.music = '';
			fadeDisable();
			dispatchEvent(new Event(TERMINATE));
		}

		// 點選某個石板
		public function onClick(index:Number):void
		{
			if (state != STATE_PICK)
			{
				return;
			}
			state = STATE_COLLECT;
			playSound('1300');
			// 按鈕消失
			_buttons[index].visible = false;
			_pos[index] = _queue[_current].Face;
			// 動畫出現
			_breaks[index].visible = true;
			_breaks[index].oStone.gotoAndPlay('Start');
			_breaks[index].oItem.gotoAndStop('Item' + _queue[_current].Face);
			_breaks[index].oItem.oAni.oFlare.visible = false;
			//_statistics[_queue[_current].Face]++;
			addCaller(_breaks[index], {time: TIME_BREAK_AND_FLARE, count: 1, onUpdate: bountyItem, onUpdateParams: [index, _queue[_current].Gold]});
			addCaller(_breaks[index], {time: TIME_BREAK_AND_FLARE, count: 1, onUpdate: flareItems, onUpdateParams: [_queue[_current].Face]});
			addCaller(_breaks[index], {time: TIME_BREAK_COLLECT, count: 1, onUpdate: gotItem, onUpdateParams: [_queue[_current].Face]});
			_current++;
			// 判斷是否已開到最後一個
			if (_current == _position)
			{
				//trace('開到尾巴了！！！');
				removeTween(this, timeless);
				//addTween(this, {timeless : 0, time : 30, onComplete : timeup,
				//transition : 'linear'});
				state = STATE_OVER;
				addCaller(this, {time: TIME_FLARE_5, count: 1, onUpdate: flare});
				return;
			}
		}
	}
}
